﻿package cn.lsg.util;

import java.io.File;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * 常用小方法
 * 
 */

public class by {

	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static String getTimeID() {
		return by.formatDate("yyyyMMddhhmmss", by.getDate());
	}

	public static String formatDate(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd  hh:mm:ss");
		return format.format(date);
	}

	public static String formatDate(String format, Date date) {
		SimpleDateFormat action = new SimpleDateFormat(format);
		return action.format(date);
	}

	public static Date parseDate(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	public synchronized static Date parseDate(long millis) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(millis);
		return c.getTime();
	}

	public synchronized static Date formatToDay(Date date) {
		return by.parseDate(by.formatDate("yyyyMMdd", date), "yyyyMMdd");
	}

	/**
	 * @param num
	 * @param format
	 * @return
	 */
	public synchronized static String formatNumber(int num, String format) {
		DecimalFormat df = new DecimalFormat(format);
		return df.format(num);
	}

	public synchronized static boolean isNull(Object obj) {
		boolean flag = false;
		if (obj == null) {
			flag = true;
		}
		if (obj instanceof String) {
			flag = "".equals(((String) obj).replace(" ", ""))
					|| "null".equals(((String) obj));
		}
		return flag;
	}

	public static String md5(String str) {
		return UtilEncrypt.MD5Encode(str);
	}

	public static String subString(String resour, int len, String end) {
		if (resour.length() > len) {
			return resour.substring(0, len) + end;
		} else {
			return resour;
		}
	}

	public static String getPath() {
		String base = by.class.getResource("/").getPath();
		base = base.replace("WEB-INF/classes/", "");
		base = base.substring(1, base.length());
		return base;
	}

	public static String getPath(String path) {
		String base = by.class.getResource(path).getPath();
		return base;
	}

	public static String parseChar(String str, String charSet) {
		String tmp = null;
		try {
			tmp = new String(str.getBytes("iso-8859-1"), charSet);
		} catch (Exception e) {
		}
		return tmp;
	}

	public static int parseInt(Object o) {
		int j = 0;
		try {
			j = Integer.parseInt(o.toString());
		} catch (Exception e) {

		}
		return j;
	}

	/**
	 * 已废弃
	 * 
	 * @return
	 * 
	 */
	public static Date getDate() {
		return by.dateInstance();
	}

	public static Date dateInstance() {
		return new Date();
	}

	public static String printStackTrace(Exception e) {
		StringBuffer sb = new StringBuffer(e.getMessage());
		StackTraceElement[] st = e.getStackTrace();
		for (StackTraceElement s : st) {
			sb.append("\r<br />");
			sb.append(s.getClassName());
			sb.append(":");
			sb.append(s.getMethodName());
			sb.append(" ");
			sb.append(s.getLineNumber());

		}
		return sb.toString();
	}

	/**
	 * 移除文件或目录
	 * 
	 * @param path
	 *            绝对路径
	 **/
	public synchronized static void removeFile(String path) {
		File file = new File(path);
		if (file.isDirectory()) {
			File[] fs = file.listFiles();
			for (File f : fs) {
				removeFile(f.getPath());
			}
		}
		if (file.isFile()) {
			file.delete();
		}
	}

}
